﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EislaufScb.Model.Repository;
using EislaufScb.Model;

namespace EislaufScb.WebForms.ViewModel.Person
{
    public class PersonViewModel
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string MiddleName { get; set; }

        public string DateOfBirth { get; set; }

        public string Email { get; set; }

        public string StreetName { get; set; }

        public string Address { get; set; }

        public int StreetNumber { get; set; }

        public string StreetExtension { get; set; }

        public int CityPostalCode { get; set; }

        public string CityName { get; set; }

        public string AreaOfResponsibility { get; set; }

        public List<string> SubscriberNumbers { get; set; }        

        public PersonViewModel()
        {

        }

        public static List<PersonViewModel> GetContacts()
        {
            List<PersonViewModel> result = new List<PersonViewModel>();

            using (BaseRepository repository = new BaseRepository(new ClubModelContainer()))
            {
                List<EislaufScb.Model.Person> persons = repository.GetAll<EislaufScb.Model.Person>().ToList()
                                                                  .Where(person => person.PersonRoleRelation
                                                                  .FirstOrDefault().PersonType.Name != "Skater" && person.PersonRoleRelation
                                                                  .FirstOrDefault().PersonType.Name != "Trainer").ToList();

                foreach (var item in persons)
                {
                    PersonViewModel person = new PersonViewModel();                    
                    person.FirstName = item.FirstName;
                    person.MiddleName = item.MiddleName;
                    person.LastName = item.LastName;
                    person.Email = item.EMail;
                    person.AreaOfResponsibility = item.AreaOfResponsibility;

                    person.SubscriberNumbers = new List<string>();
                    foreach (var phone in item.Phone)
                    {
                        person.SubscriberNumbers.Add(phone.SubScriberNumber);
                    }

                    person.StreetName = item.Address.FirstOrDefault().Street.Name;
                    person.StreetNumber = item.Address.FirstOrDefault().Street.Number;
                    person.StreetExtension = item.Address.FirstOrDefault().Street.Extension;
                    person.CityPostalCode = item.Address.FirstOrDefault().City.Postalcode;
                    person.CityName = item.Address.FirstOrDefault().City.Name;

                    string address = string.Format("{0} {1} {2} <BR/> {3} {4}", person.StreetName, person.StreetNumber, person.StreetExtension, person.CityPostalCode, person.CityName);
                    person.Address = address;

                    result.Add(person);
                }                
            }

            return result;
        }
    }
}